package com.gingko.duominuo.ui.widget

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.view.*
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.gingko.duominuo.databinding.FragmentCommonDialogBinding
import com.gingko.duominuo.utils.loge
import com.gingko.duominuo.utils.screenWidth
import com.gingko.duominuo.utils.singleClick

class CommonDialog : DialogFragment() {
    private val mViewBinding by lazy { FragmentCommonDialogBinding.inflate(layoutInflater) }
    private var title: CharSequence? = null
    private var topImage: Int? = null
    private var message: CharSequence? = null
    private var negativeText: CharSequence? = null
    private var isHasNegativeButton = true
    private var negativeClickEvent: ClickEvent? = null
    private var negativeTextColor: Int? = null
    private var positiveText: CharSequence? = null
    private var positiveClickEvent: ClickEvent? = null
    private var positiveTextColor: Int? = null
    private var canceledOnTouchOutside: Boolean = false
    private var canPressBack: Boolean = false
    private var messageGravity: Int = Gravity.CENTER

    companion object {
        fun create(): CommonDialog {
            return CommonDialog()
        }
    }

    fun setTitle(title: CharSequence): CommonDialog {
        this.title = title
        return this
    }

    fun setTopImage(topImage: Int): CommonDialog {
        this.topImage = topImage
        return this
    }

    fun setMessage(message: CharSequence): CommonDialog {
        this.message = message
        return this
    }

    fun setNegativeText(negativeText: CharSequence): CommonDialog {
        this.negativeText = negativeText
        return this
    }

    fun setHasNegativeButton(isHasNegativeButton: Boolean): CommonDialog {
        this.isHasNegativeButton = isHasNegativeButton
        return this
    }

    fun setNegativeClickListener(negativeClickEvent: ClickEvent): CommonDialog {
        this.negativeClickEvent = negativeClickEvent
        return this
    }

    fun setNegativeTextColor(negativeTextColor: Int): CommonDialog {
        this.negativeTextColor = negativeTextColor
        return this
    }

    fun setPositiveText(positiveText: CharSequence): CommonDialog {
        this.positiveText = positiveText
        return this
    }

    fun setPositiveClickListener(positiveClickEvent: ClickEvent): CommonDialog {
        this.positiveClickEvent = positiveClickEvent
        return this
    }

    fun setPositiveTextColor(positiveTextColor: Int): CommonDialog {
        this.positiveTextColor = positiveTextColor
        return this
    }

    fun setCanceledOnTouchOutside(canceledOnTouchOutside: Boolean): CommonDialog {
        this.canceledOnTouchOutside = canceledOnTouchOutside
        return this
    }

    fun setCanPressBack(canPressBack: Boolean): CommonDialog {
        this.canPressBack = canPressBack
        return this
    }

    fun setMessageGravity(gravity: Int): CommonDialog {
        this.messageGravity = gravity
        return this
    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog?.run {
            window?.requestFeature(Window.FEATURE_NO_TITLE)
            setCanceledOnTouchOutside(canceledOnTouchOutside)
            setCancelable(canPressBack)
        }
        isCancelable = canPressBack
        return mViewBinding.root
    }

    override fun onResume() {
        super.onResume()
        dialog?.window?.run {
            setLayout((screenWidth * 0.86).toInt(), ViewGroup.LayoutParams.WRAP_CONTENT)
            attributes.dimAmount = 0.6f
            addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
            setBackgroundDrawable(ColorDrawable(ContextCompat.getColor(requireContext(), android.R.color.transparent)))
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mViewBinding.tvDialogTitle.isVisible = !title.isNullOrEmpty()
        title?.let { mViewBinding.tvDialogTitle.text = it }
        topImage?.let {
            val topImageDrawable = ContextCompat.getDrawable(requireContext(), it)
            mViewBinding.tvDialogMessage.setCompoundDrawablesWithIntrinsicBounds(null, topImageDrawable, null, null)
        }
        mViewBinding.tvDialogMessage.isVisible = !message.isNullOrEmpty()
        mViewBinding.tvDialogMessage.movementMethod = LinkMovementMethod.getInstance()
        mViewBinding.tvDialogMessage.highlightColor = ContextCompat.getColor(requireContext(), android.R.color.transparent)
        message?.let { mViewBinding.tvDialogMessage.text = it }
        mViewBinding.tvDialogMessage.gravity = messageGravity
        mViewBinding.tvDialogNegative.isVisible = isHasNegativeButton
        negativeText?.let { mViewBinding.tvDialogNegative.text = it }
        negativeTextColor?.let { mViewBinding.tvDialogNegative.setTextColor(it) }
        mViewBinding.tvDialogNegative.singleClick {
            negativeClickEvent?.onClickEvent()
            dismiss()
        }
        positiveText?.let { mViewBinding.tvDialogPositive.text = it }
        positiveTextColor?.let { mViewBinding.tvDialogPositive.setTextColor(it) }
        mViewBinding.tvDialogPositive.singleClick {
            positiveClickEvent?.onClickEvent()
            dismiss()
        }
    }

    override fun show(manager: FragmentManager, tag: String?) {
        try {
            if (isAdded) {
                manager.beginTransaction().show(this).commitAllowingStateLoss()
            } else {
                manager.beginTransaction().remove(this).commitAllowingStateLoss()
                manager.beginTransaction().add(this, tag).commitAllowingStateLoss()
            }
        } catch (exception: Exception) {
            exception.loge()
        }
    }

    interface ClickEvent {
        fun onClickEvent()
    }
}